package com.water_work_form.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deepoove.poi.XWPFTemplate;
import com.water_work_form.entity.RecordCoagulation;
import com.water_work_form.mapper.*;
import com.water_work_form.service.RecordCoagulationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * <p>
 * 记录——混凝实验原始记录 服务实现类
 * </p>
 *
 * @author xuke
 * @since 2023-08-25
 */
@Service
public class RecordCoagulationServiceImpl extends ServiceImpl<RecordCoagulationMapper, RecordCoagulation> implements RecordCoagulationService {

    @Autowired
    RecordCoagulationMapper recordCoagulationMapper;

    @Autowired
    BasicFormMapper basicFormMapper;

    @Value("${file.templateFilePath}")
    private String templateFilePath;

    @Value("${file.destFilePath}")
    private String destFilePath;

    /**
     * 记录——混凝实验原始记录 项目查询
     * @param checkSite 检测地点（check_site）若非空则进行模糊查询
     * @param current 分页 当前页
     * @param startSampleDate 取样日期（sample_date）范围查询开始日期 若非空查询sample_date大于startSampleDate数据
     * @param endSampleDate 取样日期（sample_date）范围查询结束日期 若非空查询sample_date小于endSampleDate数据
     * @param size 分页 每页数据量
     * @param status 状态
     * @return 返回对应数据 按照状态（status）、采样时间（sample_date）顺序进行升序排序
     */
    @Override
    public IPage<RecordCoagulation> selectRecordCoagulation(String checkSite, String status, String startSampleDate, String endSampleDate, Integer current, Integer size){
        QueryWrapper<RecordCoagulation> queryWrapper = new QueryWrapper<>();
        if(!checkSite.isEmpty()){
            queryWrapper.like("sample_site", checkSite);
        }
        if(!status.isEmpty()){
            queryWrapper.eq("status", status);
        }
        if(!startSampleDate.isEmpty() && !endSampleDate.isEmpty()){
            queryWrapper.ge("sample_date", startSampleDate).le("sample_date", endSampleDate);
        }
        queryWrapper.orderByAsc("status", "sample_date");
        IPage<RecordCoagulation> page = new Page<>(current, size);
        return this.page(page, queryWrapper);
    }

    /**
     * 记录——混凝实验原始记录 基础表数据查询
     * @param name form表name
     * @return 返回对应数据 键名为表名
     */
    @Override
    public Map<String,Object> selectBasicCoagulation(String name) {
        Map<String,Object> map = new HashMap<>();
        map.put("form", basicFormMapper.getOneByName(name));
        return map;
    }

    /**
     * 记录——混凝实验原始记录 数据表导出
     * @param recordCoagulation 需要导出的数据表
     * @throws IOException 异常
     */
    @Override
    public void downloadRecordCoagulation(RecordCoagulation recordCoagulation) throws IOException {
        Map<String, Object> data = new HashMap<String, Object>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); //日期格式化
        JSONObject frontDealMessage =  JSON.parseObject(recordCoagulation.getFrontDealMessage());
        JSONObject feCl3 =  JSON.parseObject(recordCoagulation.getFeCl3());
        JSONObject pac =  JSON.parseObject(recordCoagulation.getPac());
        JSONObject pam =  JSON.parseObject(recordCoagulation.getPam());
        JSONObject turbidity =  JSON.parseObject(recordCoagulation.getTurbidity());
        data.put("sampleDate", dateFormat.format(recordCoagulation.getSampleDate()));
        data.put("feCl3", feCl3);
        data.put("pac", pac);
        data.put("pam", pam);
        data.put("turbidity", turbidity);
        data.put("frontDealMessage", frontDealMessage);
        data.put("list", recordCoagulation);
        XWPFTemplate compile = XWPFTemplate.compile(templateFilePath + "RecordCoagulation.docx");
        compile.render(data);
        compile.writeToFile(destFilePath + recordCoagulation.getId() + "out_recordCoagulation.docx");
        compile.close();
    }



}
